package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.DictionaryItem;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.DictionaryItemQueryObject;
import cn.wolfcode.crm.service.IDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("dictionaryItem")
@Controller
public class DictionaryItemController {
    @Autowired
    private IDictionaryItemService dictionaryItemService;


    @RequestMapping("")
    @RequiresPermissions(":index")
    @PermissionName("数据词典明细列表")
    public String index() {
        //只负责跳转到dictionaryItem.jsp页面即可
        return "dictionaryItem";
    }

    @RequestMapping("list")
    @RequiresPermissions("dictionaryItem:list")
    @PermissionName("数据词典明细数据")
    @ResponseBody
    public PageResult list(DictionaryItemQueryObject qo) {
        PageResult result = dictionaryItemService.query(qo);
        return result;
    }

    @RequestMapping("select")
    @RequiresPermissions("dictionaryItem:select")
    @PermissionName("数据词典明细数据")
    @ResponseBody
    public List select(DictionaryItemQueryObject qo)
    {
        return dictionaryItemService.select(qo);
    }

    @RequestMapping("delete")
    @RequiresPermissions("dictionaryItem:delete")
    @PermissionName("数据词典明细删除")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            dictionaryItemService.deleteByPrimaryKey(id);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions("dictionaryItem:saveOrUpdate")
    @PermissionName("数据字典工保存")
    @ResponseBody
    public JsonResult saveOrUpdate(DictionaryItem dictionaryItem) {
        JsonResult result = new JsonResult();
        try {
            if (dictionaryItem.getId() == null) {
                dictionaryItemService.insert(dictionaryItem);
                result.setMessage("保存成功");
            } else {
                dictionaryItemService.updateByPrimaryKey(dictionaryItem);
                result.setMessage("编辑成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }
}
